<?php

class Class_sbps {
	public $objSpsUtils;
	public $XMLStr;

	public $url_sps_api;
	public $sps_api_request;
	public $sps_api_request2;
	public $merchant_id;
	public $service_id;
	public $id_sps_hash;
	public $key_sps_3des;
	public $key_sps_3des_iv;
	public $NG_msg;
	function loadinclude()
	{
		require_once("../system/sbps/sbps_config.php");
		require_once("../system/sbps/LC_Util_SPS.php");
		require_once("../system/sbps/sbps_NGmsg.php");
	}

function trysend($cardNO,$carddate) {

	$this->loadinclude();
	$this->objSpsUtils = new LC_Util_SPS;
	// XML用にデータを配列化
	$arrXml = array("merchant_id" => $this->merchant_id,
			"service_id" => $this->service_id,
			"cust_code" => "9999999999",//"00001",
			"order_id" => "9999999999". date('YmdHis'),
			"item_id" => "999",
			'item_name'=>"",
			"tax" => 1,
			"amount" => 10,
			"free1" => "9999999999",
			//"order_rowno"=>1,
			"pay_method_info" => array("cc_number" => $cardNO,
					"cc_expiration" => $carddate,//"201305",
					"dealings_type" => "10",
					"divide_times" => "",//($divide_times > 0) ? sprintf("%03d", $divide_times) : "",
					//"bonus_details_times" => $arrInput['bonus_details_times'],
					"cust_manage_flg" => 0//sprintf("%01d", $arrInput['stock'])
			),
			"encrypted_flg" => 1,
			"request_date" => date('YmdHis'),
			"limit_second" => 6000);
	//var_dump($arrXml);return;
	// ハッシュ生成
	$arrXml['sps_hashcode'] = $this->objSpsUtils->createHashcode($arrXml, $this->id_sps_hash);
	//$arrXml['item_name']=base64_encode($arrXml['item_name']);
	// 固有情報を暗号化
	$arrXml = $this->objSpsUtils->encryptData($arrXml, "pay_method_info", $this->key_sps_3des,$this->key_sps_3des_iv);
	// マルチバイト文字列のエンコード
	$arrXml = $this->objSpsUtils->encodeMbstring($arrXml, array("free1"));
	$arrXml = $this->objSpsUtils->encodeMbstring($arrXml, array("item_name"));
	// XML生成
	$requestXml = $this->objSpsUtils->changeArray2Xml($arrXml, $this->sps_api_request);

	// リクエスト
	list($r_code, $response) = $this->objSpsUtils->sendRequestSps($requestXml, $this->url_sps_api,$this->merchant_id,$this->service_id,$this->id_sps_hash);

	//$array_result=$this->objSpsUtils->changeXml2Array($response);

	//$this->checkRespoinse($r_code,$response);

	$ret=$this->checkRespoinse($r_code,$response);

	if($ret=="")
	{
		$_SESSION["sbps_cardresponse"]=array($r_code,$response);
		return "";
	}

	else
	{
		return $ret;
	}

}

	function sendST01($cardNO,$carddate,$orderID,$cust_code="99999999999999999999") {

	$this->loadinclude();
	$this->objSpsUtils = new LC_Util_SPS;
	// XML用にデータを配列化
	$arrXml = array("merchant_id" => $this->merchant_id,
			"service_id" => $this->service_id,
			"cust_code" => $cust_code,//"00001",
			"order_id" => $orderID. date('YmdHis'),
			"item_id" => "999",
			'item_name'=>"",
			"tax" => 1,
			"amount" => 10,
			"free1" => $orderID,
			//"order_rowno"=>1,
			"pay_method_info" => array("cc_number" => $cardNO,
					"cc_expiration" => $carddate,//"201305",
					"dealings_type" => "10",
					"divide_times" => "",//($divide_times > 0) ? sprintf("%03d", $divide_times) : "",
					//"bonus_details_times" => $arrInput['bonus_details_times'],
					"cust_manage_flg" => 0//sprintf("%01d", $arrInput['stock'])
			),
			"encrypted_flg" => 1,
			"request_date" => date('YmdHis'),
			"limit_second" => 6000);
	//var_dump($arrXml);return;
	// ハッシュ生成
	$arrXml['sps_hashcode'] = $this->objSpsUtils->createHashcode($arrXml, $this->id_sps_hash);
	//$arrXml['item_name']=base64_encode($arrXml['item_name']);
	// 固有情報を暗号化
	$arrXml = $this->objSpsUtils->encryptData($arrXml, "pay_method_info", $this->key_sps_3des,$this->key_sps_3des_iv);
	// マルチバイト文字列のエンコード
	$arrXml = $this->objSpsUtils->encodeMbstring($arrXml, array("free1"));
	$arrXml = $this->objSpsUtils->encodeMbstring($arrXml, array("item_name"));
	// XML生成
	$requestXml = $this->objSpsUtils->changeArray2Xml($arrXml, $this->sps_api_request);

	// リクエスト
	list($r_code, $response) = $this->objSpsUtils->sendRequestSps($requestXml, $this->url_sps_api,$this->merchant_id,$this->service_id,$this->id_sps_hash);

	//$array_result=$this->objSpsUtils->changeXml2Array($response);

	//$this->checkRespoinse($r_code,$response);

	$ret=$this->checkRespoinse($r_code,$response);

	if($ret=="")
	{
		$_SESSION["sbps_cardresponse"]=array($r_code,$response);
		return "";
	}

	else
	{
		return $ret;
	}

}

	function sendST02()
	{
		$this->loadinclude();
		if(empty($_SESSION["sbps_cardresponse"]))
		{
			return "SESSION_Lost";
		}
		// 確定処理
		$this->exeSpsCreditCommit($_SESSION["sbps_cardresponse"][0], $_SESSION["sbps_cardresponse"][1]);

	}


	/**
	 * 確定処理
	 *
	 * @param int $r_code
	 * @param xml $response
	 * @param int $payment_total
	 */
	function exeSpsCreditCommit($r_code, $response) {
		// レスチェック
		$arrRes;
		$ret = $this->checkRespoinse($r_code, $response,$arrRes);
		if ($ret === false) {
			return "SESSION_Lost";
		}

		// トラッキングIDを一時受注データに保持
		//$this->objHelperDB->sfRegistTempOrder($this->uniqid, array("memo06" => $arrRes['res_tracking_id']));
		// XML用にデータを配列化
		$arrXml = array("merchant_id" => $this->merchant_id,
				"service_id" => $this->service_id,
				"sps_transaction_id" => $arrRes['res_sps_transaction_id'],
				"tracking_id" => $arrRes['res_tracking_id'],
				"processing_datetime" => $arrRes['res_process_date'],
				"request_date" => date('YmdHis'),
				"limit_second" => 6000);
		// ハッシュ生成
		$arrXml['sps_hashcode'] = $this->objSpsUtils->createHashcode($arrXml, $this->id_sps_hash);
		// XML生成
		$requestXml = $this->objSpsUtils->changeArray2Xml($arrXml, $this->sps_api_request2);
		// リクエスト
		list($r_code, $response) = $this->objSpsUtils->sendRequestSps($requestXml, $this->url_sps_api,$this->merchant_id,$this->service_id,$this->id_sps_hash);

		//echo $response;
		// 完了処理
		//$this->exeFix($r_code, $response, $payment_total);
	}
	/**
	 * レスチェック処理
	 *
	 * @param int $r_code
	 * @param xml $response
	 */
	function checkRespoinse($r_code, $response,&$arrRes=array()) {
		// 通信エラー
		if ($r_code != 200) {
			return "SBPSとの通信に失敗しました。";
			// レス処理
		} else {
			// XMLを配列化
			$arrRes = $this->objSpsUtils->changeXml2Array($response);
			// 成功
			if($arrRes['res_result'] == "OK") {
				return "";
				// 失敗
			} else {
				$err = "決済に失敗しました。";
				$err .= empty($this->NG_msg[substr($arrRes["res_err_code"], 3,2)])?"errcode-".substr($arrRes["res_err_code"], 3,2).":SBPS 内部システムエラー":$this->NG_msg[substr($arrRes["res_err_code"], 3,2)];
				return $err;
		}
	}
	}
}